package StudyCode.summary.regularexpressionsmatch;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddingCommentsToARegularExpression {
	public static void main(String[] argv) {
		CharSequence inputStr = "a b";
		String patternStr = "a b";

		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(inputStr);
		boolean matchFound = matcher.matches();
		System.out.println(matchFound);

		pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
		matcher = pattern.matcher(inputStr);
		matchFound = matcher.matches(); // false
		System.out.println(matchFound);

		patternStr = "a  [\\ ]  b";
		pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
		matcher = pattern.matcher(inputStr);
		matchFound = matcher.matches(); // true
		System.out.println(matchFound);

		matchFound = pattern.matches("a b", inputStr); // true
		System.out.println(matchFound);
		matchFound = pattern.matches("(?x)a b", inputStr); // false
		System.out.println(matchFound);
		matchFound = pattern.matches("(?x)a [\\ ] b", inputStr); // true
		System.out.println(matchFound);
		matchFound = pattern.matches("(?x)a \\s b", inputStr); // true
		System.out.println(matchFound);
		matchFound = pattern.matches("a (?x:  b   )", inputStr); // true
		System.out.println(matchFound);

		matchFound = pattern.matches("(?x)a \t\n \\s b", inputStr); // true
		System.out.println(matchFound);
		try {
			File f = new File("pattern.txt");
			FileReader rd = new FileReader(f);
			char[] buf = new char[(int) f.length()];
			rd.read(buf);
			patternStr = new String(buf);

			matcher = pattern.matcher(inputStr);
			matchFound = matcher.matches(); // true
		} catch (IOException e) {
		}
	}
}
